﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SmartSpace.Dynamic.Controls;

namespace SmartSpace.WpfShell.Modules.Dynamic.Controls
{
    /// <summary>
    /// Interaction logic for IdentityControl.xaml
    /// </summary>
    public partial class IdentityControl : UserControl, ISsControl
    {
        public string Identity
        {
            get { return (string)GetValue(IdentityProperty); }
            set { SetValue(IdentityProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IdentityProperty.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IdentityProperty =
            DependencyProperty.Register("Identity", typeof(string), typeof(IdentityControl), new PropertyMetadata(default(string), OnIdentityPropertyChangedCallback));

        static void OnIdentityPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            IdentityControl control = d as IdentityControl;
            control._ctlEditIdentity.Text = (string)e.NewValue;
        }

        public IdentityControl()
        {
            InitializeComponent();
        }

        private void _ctlEditIdentity_TextChanged(object sender, TextChangedEventArgs e)
        {
            this.Identity = _ctlEditIdentity.Text;
        }

        #region ISsControl.IsReadOnly
        public bool IsReadOnly
        {
            get { return (bool)GetValue(IsReadOnlyProperty); }
            set { SetValue(IsReadOnlyProperty, value); }
        }

        public static readonly DependencyProperty IsReadOnlyProperty = DependencyProperty.Register(
            "IsReadOnly",
            typeof(bool),
            typeof(IdentityControl),
            new PropertyMetadata(default(bool), OnIsReadOnlyPropertyChangedCallback));

        private static void OnIsReadOnlyPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            IdentityControl control = d as IdentityControl;
            bool isReadOnly = (bool)e.NewValue;
            control._ctlEditIdentity.IsEnabled = !isReadOnly;
        }
        #endregion
    }
}
